﻿/*
 * Created by SharpDevelop.
 * User: v-edwild
 * Date: 05/07/2007
 * Time: 06:53
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Collections.ObjectModel;
using NUnit.Framework;
using Wax;

namespace WaxLibTest
{
	/// <summary>
	/// Tests all the behavior of the <see cref="ValidateCommand" /> class.
	/// </summary>
	[TestFixture]
	public class ValidateCommandTest
	{

        /// <summary>
        /// Test that we can validate the attributes of a given element.
        /// </summary>
        [Test]
        public void AttributeBasedValidation()
        {
            using (TestManager testManager = new TestManager())
            {
                ValidateCommand attributeValidation = (ValidateCommand)CreateCommandValidate("mainForm,Attribute:class", "ById", "form1");
                Assert.AreEqual("Attribute:class", attributeValidation.PropertyName);
                Assert.AreEqual("ClassName", attributeValidation.AttributeName);

                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(attributeValidation);

                testManager.Execute();
            }	            
        }

        /// <summary>
        /// Tests that we can validate the 'Value' property of an element.
        /// </summary>
	    [Test]
	    public void ExecuteValidateValue()
	    {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(SetValueCommandTest.CreateCommandText("Edward,Text", "ById", "FirstName"));
                testManager.Tests[0].Commands.Add(CreateCommandValidate("Edward,Value", "ById", "FirstName"));

                testManager.Execute();
            }	        
	    }

        /// <summary>
        /// Tests using the ValidateLastFound command.
        /// </summary>
        [Test]
        public void ValidateLastFound()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdSetCookie", "SetCookie", "http://www.google.co.uk/,\"wax=1234; expires=Wed, 01-Jan-2020 00:00:00 GMT\"", string.Empty, string.Empty }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdGetCookie", "GetCookie", "\"http://www.google.co.uk/\",\"wax\"", string.Empty, string.Empty }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "CmdValidateLastFound", "ValidateLastFound", "wax=1234", string.Empty, string.Empty }));
                
                testManager.Execute();
            }
        }

        /// <summary>
        /// Tests that we can validate the selected value of a drop down list.
        /// </summary>
        [Test]
        public void ExecuteValidateSelectedValue()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "SetCardList", "SetValue", "2,SelectedItem", "ById", "cardList" }));
                testManager.Tests[0].Commands.Add(CreateCommandValidate("2,SelectedItem", "ByLastElement", string.Empty));

                testManager.Execute();
            }
        }

        /// <summary>
        /// Tests that we can validate the multiple selected values of a drop down list.
        /// </summary>
        [Test]
        public void ExecuteValidateSelectedValues()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "SetCardList", "SetValues", "1,2,3,SelectedItem", "ById", "cardList2" }));
                testManager.Tests[0].Commands.Add(CreateCommandValidateItems("1,2,3,SelectedItem", "ByLastElement", string.Empty));

                testManager.Execute();
            }
        }

        /// <summary>
        /// Tests that we can validate the multiple selected values of a drop down list.
        /// </summary>
        [Test, ExpectedException(typeof(ValidateException))]
        public void ExecuteValidateSelectedValuesFails()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "SetCardList", "SetValue", "2,SelectedItem", "ById", "cardList" }));
                testManager.Tests[0].Commands.Add(CreateCommandValidateItems("1,2,3,SelectedItem", "ByLastElement", string.Empty));

                testManager.Execute();
            }
        }

		/// <summary>
		/// Tests the execution of the validate command, this tests asserts for the 'Text' value of link1 and
		/// asserts that it's equal to 'Bob' - which it isn't so should through an Exception
		/// </summary>
		[Test]
		public void ExecuteValidatePasses()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CreateCommandValidate("Go to test page 2,Text", "ById", "link1"));
				
				testManager.Execute();
			}
		}
		
		/// <summary>
		/// Tests that the expected error is thrown when an unknown property is given
		/// </summary>
		[Test, ExpectedExceptionAttribute(typeof(ArgumentOutOfRangeException))]
		public void InvalidPropertyName()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CreateCommandValidate("Go to test page 2,ITextUnknown", "ById", "link1"));
				
				testManager.Execute();
			}
		}
		
		/// <summary>
		/// Tests the execution of the validate command, this tests asserts for the 'Text' value of link1 and
		/// asserts that it's equal to 'Bob' - which it isn't so should through an Exception
		/// </summary>
		[Test, ExpectedExceptionAttribute(typeof(ValidateException))]
		public void ExecuteValidateFails()
		{
			using (TestManager testManager = new TestManager())
			{
				testManager.Tests.Add(new Test());
				testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
				testManager.Tests[0].Commands.Add(CreateCommandValidate("Bob,Text", "ById", "link1"));
				
				testManager.Execute();
			}
		}

        /// <summary>
        /// Tests the <see cref="ValidateCommand.IsRegularExpression"/>
        /// </summary>
        [Test]
        public void IsRegularExpression()
        {
            string expectedValue = @"\w*google";

            ValidateCommand validateCommand = new ValidateCommand();
            validateCommand.Arguments.Add(expectedValue);
            validateCommand.Arguments.Add("Text");

            Assert.IsTrue(validateCommand.IsRegularExpression, "IsRegularExpression did not return the correct value.");

            validateCommand.Arguments.Clear();
            validateCommand.Arguments.Add("not a reg ex.");
            validateCommand.Arguments.Add("Text");
            Assert.IsFalse(validateCommand.IsRegularExpression, "IsRegularExpression did not return the correct value.");

        }

		#region Private static methods
		
		/// <summary>
		/// Create a command that validates some property of an <see cref="WatiN.Core.Element">element</see>.
		/// </summary>
		/// <param name="commandArgs">Validate command arguments</param>
		/// <param name="findMethodType">Find method type</param>
		/// <param name="findMethodArgs">Find method arguments</param>
		/// <returns></returns>
		internal static Command CreateCommandValidate(string commandArgs, string findMethodType, string findMethodArgs)
		{
			return CommandFactory.Parse(new string[] {"ValidateCommand1", "Validate", commandArgs, findMethodType, findMethodArgs});
		}

        /// <summary>
        /// Create a command that validates some property of an <see cref="WatiN.Core.Element">element</see>.
        /// </summary>
        /// <param name="commandArgs">Validate command arguments</param>
        /// <param name="findMethodType">Find method type</param>
        /// <param name="findMethodArgs">Find method arguments</param>
        /// <returns></returns>
        internal static Command CreateCommandValidateItems(string commandArgs, string findMethodType, string findMethodArgs)
        {
            return CommandFactory.Parse(new string[] { "ValidateCommand1", "ValidateItems", commandArgs, findMethodType, findMethodArgs });
        }
		
		#endregion
	}
}
